package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.TreeResult;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private UserDao userDao;

    @Override
    public List<TreeResult> findAll() {
        List<Role> list = roleDao.findAll();

        List<TreeResult> treeResults = new ArrayList<>();

        if(list != null && list.size() > 0) {
            for (Role role : list) {
                TreeResult result = new TreeResult();
                result.setId(role.getId());
                result.setName(role.getName());
                treeResults.add(result);
            }
        }
        return treeResults;
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);

        //根据条件查询结果
        Page<Role> RoleList = roleDao.findByCondition(queryString);

        return new PageResult(RoleList.getTotal(),RoleList.getResult());
    }

    @Override
    public Map findByRoleId(Integer id) {
        Role role = roleDao.findByRoleId(id);
        Map map = new HashMap();

        map.put("users",role.getUsers());
        map.put("permissions",role.getPermissions());

        List<Menu> menus = menuDao.findAll();
        map.put("menus",menus);

        return map;
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public Map findAllByRoleId() {
        List<Menu> menus = menuDao.findAllMenu();
        List<Permission> permissions = permissionDao.findAllPermission();
        Map map = new HashMap();
        map.put("menus",menus);
        map.put("permissions",permissions);
        return map;
    }

    @Override
    public Map findIdByRoleId(Integer id) {
        List<Integer> menuIds = menuDao.findByIds(id);
        List<Integer> permissionIds = permissionDao.findByIds(id);
        Map map = new HashMap();
        map.put("menuIds",menuIds);
        map.put("permissionIds",permissionIds);
        return map;
    }

    @Override
    public void edit(Integer[] permissionIds, Integer[] menus, Role role) {
        //先删除依赖
        permissionDao.deleteByRoleId(role.getId());
        if(permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                permissionDao.insertByRoleId(permissionId,role.getId());
            }
        }

        menuDao.deleteByRoleId(role.getId());
        if(menus != null && menus.length > 0) {
            for (Integer menu : menus) {
                menuDao.insertByRoleId(menu,role.getId());
            }
        }

        roleDao.edit(role);
    }

    @Override
    public List<Role> getRoleList(String username) {
        User user = userDao.findByUsername(username);
        return roleDao.getRoleList(user.getId());
    }
}
